package com.webcash.CNBoard.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.webcash.CNBoard.Model.Work;
import com.webcash.CNBoard.Model.WorkTracer;

@Repository
public class WorkTracerRepositoryImpl extends SqlSessionDaoSupport implements WorkTracerRepository {

	public void insertWorkTracer(WorkTracer tracer) {
		SqlSession session = getSqlSession();
		
		session.insert("WorkTracer.insertIntoWorkTracer", tracer);
	}

	public WorkTracer selectWorkTracerByDownerWorkNo(int workNo) {
		SqlSession session = getSqlSession();
		
		return session.selectOne("WorkTracer.selectWorkTracerByDownerWorkNo", workNo);
	}

	public WorkTracer getWorkTracerByRelationNo(int relationNo) {
		SqlSession session =getSqlSession();
		
		return session.selectOne("WorkTracer.selectWorkTracerByRelationNo", relationNo);
	}

	public void modifyWorkTracer(WorkTracer tracer) {
		SqlSession session = getSqlSession();
		
		Map map = new HashMap();
		map.put("upperWorkNo", tracer.getUpperWorkNo());
		map.put("upperProjectNo", tracer.getUpperProjectNo());
		map.put("relationNo", tracer.getRelationNo());
		
		session.update("WorkTracer.updateWorkTracerByRelationNo", map);
	}

	public void deleteWorkTracerByRelationNo(int relationNo) {
		SqlSession session = getSqlSession();
		
		session.delete("WorkTracer.deleteWorkTracerByRelationNo", relationNo);
		
	}

	public List<Work> selectDownerWorkListByProjectNoAndWorkNo(int projectNo,
			int workNo) {
		SqlSession session = getSqlSession();
		
		Map params = new HashMap();
		params.put("projectNo", projectNo);
		params.put("workNo", workNo);
		return session.selectList("WorkTracer.selectDownerWorkListByProjectNoAndWorkNo", params);
	}

	public void insertWorkTracerWithUpperwork(int workNo, int projectNo,
			int upperWorkNo) {
		SqlSession session = getSqlSession();
		
		WorkTracer workTracer = new WorkTracer();
		
		workTracer.setUpperWorkNo(upperWorkNo);
		workTracer.setUpperProjectNo(projectNo);
		workTracer.setDownerWorkNo(workNo);
		workTracer.setDownerProjectNo(projectNo);
		workTracer.setRelationType("상위일감");
		
		session.insert("WorkTracer.insertIntoWorkTracer", workTracer);
		
		
	}

	public List<Work> getWorkListByProjectNo(int projectNo) {
		SqlSession session = getSqlSession();
		return session.selectList("WorkTracer.selectWorkTracerListByProjectNo", projectNo);
	}

	public void deleteWorkTracerByWorkNo(int workNo) {
		SqlSession session = getSqlSession();
		session.delete("WorkTracer.deleteWorkTracerByWorkNo", workNo);
	}
	
	
	
	
}
